package com.yigang.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yigang.dao.ClassificationDao;
import com.yigang.dao.ProductMapper;
import com.yigang.entity.Classification;
import com.yigang.entity.Product;
import com.yigang.service.ClassificationService;
import com.yigang.utils.ResultUtil;

@Service
@Transactional
public class ClassificationServiceImpl implements ClassificationService {
	@Autowired
	private ClassificationDao classificationDao;

	@Autowired
	private ProductMapper productMapper;

	@Override
	public ResultUtil selectClassificationList(Integer page, Integer limit, Classification record) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page, limit);
		List<Classification> classification = classificationDao.selectClassificationList(record);
		PageInfo<Classification> pageInfo = new PageInfo<Classification>(classification);
		ResultUtil resultUtil = new ResultUtil();
		resultUtil.setCode(0);
		resultUtil.setCount(pageInfo.getTotal());
		resultUtil.setData(pageInfo.getList());
		return resultUtil;
	}

	@Override
	public ResultUtil deleteByPrimaryKey(Integer classification_id) {
		// TODO Auto-generated method stub
		List<Product> productList = productMapper.getProductIdByClassificationId(classification_id);
		if (productList.size() > 0) {
			return ResultUtil.error("该分类下存在商品");
		} else {
			classificationDao.deleteByPrimaryKey(classification_id);
			return ResultUtil.ok();
		}

	}

	@Override
	public ResultUtil insertSelective(Classification record) {
		// TODO Auto-generated method stub
		classificationDao.insertSelective(record);
		return ResultUtil.ok();
	}

	@Override
	public ResultUtil updateByPrimaryKeySelective(Classification record) {
		// TODO Auto-generated method stub
		classificationDao.updateByPrimaryKeySelective(record);

		return ResultUtil.ok();
	}

	@Override
	public Classification selectClassificationById(int classification_id) {
		// TODO Auto-generated method stub
		return classificationDao.selectClassificationById(classification_id);
	}

	@Override
	public ResultUtil updateClassificationStatusById(int classification_id, int classification_display) {
		// TODO Auto-generated method stub
		classificationDao.updateClassificationStatusById(classification_id, classification_display);
		return ResultUtil.ok();
	}

	@Override
	public List<Map<String, Object>> getAll() {
		// TODO Auto-generated method stub

		return classificationDao.getAll();
	}

}
